#include "componentwindow.h"
#include "componentgeneral.h"
#include "componentpinmanager.h"
#include "qedacomponent.h"
#include "librarymodel.h"
#include "../appsettings.h"

#include <QtGui/qboxlayout.h>
#include <QtGui/qbuttongroup.h>
#include <QtGui/qevent.h>
#include <QtGui/qgroupbox.h>
#include <QtGui/qicon.h>
#include <QtGui/qmessagebox.h>
#include <QtGui/qpushbutton.h>
#include <QtGui/qradiobutton.h>
#include <QtGui/qsplitter.h>
#include <QtGui/qtabwidget.h>
//#include <QtGui/qtextedit.h>

ComponentWindow::ComponentWindow(LibraryModel *libModel, QedaComponent *component, QWidget *parent) :
    QSplitter(parent)
{
    m_modified = false;
    m_libModel = libModel;
    m_component = component;

    setOrientation(Qt::Horizontal);
    setAttribute(Qt::WA_DeleteOnClose);

    m_applyButton = new QPushButton(QIcon(":/images/attention.png"), tr("Apply"));
    m_applyButton->setDisabled(true);
    QFont boldFont = m_applyButton->font();
    boldFont.setBold(true);
    m_applyButton->setFont(boldFont);
    connect(m_applyButton, SIGNAL(clicked()), this, SLOT(apply()));

    m_restoreButton = new QPushButton(tr("Restore"));
    m_restoreButton->setDisabled(true);
    connect(m_restoreButton, SIGNAL(clicked()), this, SLOT(restore()));

    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    buttonsLayout->addWidget(m_applyButton);
    buttonsLayout->addWidget(m_restoreButton);

    m_propertiesTab = new QTabWidget;
    m_tabs.insert("General", createGeneralTab());
    m_tabs.insert("PinManager", new ComponentPinManager);
    m_tabs.insert("Symbol", createSymbolTab());
    m_tabs.insert("Pattern", createPatternTab());

    QVBoxLayout *propertiesLayout = new QVBoxLayout;
    propertiesLayout->addLayout(buttonsLayout);
    propertiesLayout->addWidget(m_propertiesTab, 1);

    QWidget *propertiesWidget = new QWidget;
    propertiesWidget->setLayout(propertiesLayout);

    m_symbolPreview = new QWidget;

    m_patternPreview = new QWidget;

    m_previewSplitter = new QSplitter;
    m_previewSplitter->setOrientation(Qt::Vertical);
    m_previewSplitter->addWidget(m_symbolPreview);
    m_previewSplitter->addWidget(m_patternPreview);

    addWidget(propertiesWidget);
    addWidget(m_previewSplitter);

    qreal tabWidth = g_appSettings->asDouble("ComponentWindow/SplitterRatio", 1.0/3);
    setSizes(QList<int>() << qRound(tabWidth * 1000) << qRound((1 - tabWidth) * 1000));

    restore();
}

void ComponentWindow::apply()
{
    m_component->setFlags(static_cast<QedaComponent::ComponentFlags>(m_symbolPattern->checkedId()),
                          QedaComponent::SymbolPatternFlags);
    m_general->dataTo(m_component);
    setModified(false);
}

void ComponentWindow::changeComponentFlags(int componentFlags)
{
    updateTabs(componentFlags);
    setModified(true);
}

void ComponentWindow::closeEvent(QCloseEvent *event)
{
    g_appSettings->set("ComponentWindow/SplitterRatio",
                       (qreal)sizes().first() / (sizes().first() + sizes().last()));
    g_appSettings->writeSetting("ComponentWindow/SplitterRatio");

    if (m_modified)
    {
        int res = QMessageBox::question(this, tr("Unapplied Changes"),
                                        QString(tr("There are unapplied parameters changes in component: <b>%1</b>"))
                                        .arg(m_component->name())
                                        + "<br><br>" + tr("Apply it now?"),
                                        QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
                                        QMessageBox::Cancel);
        if (res == QMessageBox::Cancel)
            event->ignore();
        else
        {
            if (res == QMessageBox::Yes)
                apply();
            else
                restore();
            m_component->setWin(0);
            event->accept();
        }
    }
    else
    {
        m_component->setWin(0);
        event->accept();
    }
}

QWidget *ComponentWindow::createGeneralTab()
{
    m_general = new ComponentGeneral(m_libModel);

    m_symbolPattern = new QButtonGroup(this);
    m_symbolPattern->addButton(new QRadioButton(tr("Symbol + Pattern")),
                               QedaComponent::SymbolPatternFlags);
    m_symbolPattern->addButton(new QRadioButton(tr("Symbol only")), QedaComponent::SymbolFlag);
    m_symbolPattern->addButton(new QRadioButton(tr("Pattern only")), QedaComponent::PatternFlag);
    m_symbolPattern->button(QedaComponent::SymbolFlag | QedaComponent::PatternFlag)->setChecked(true);
    connect(m_symbolPattern, SIGNAL(buttonClicked(int)), this, SLOT(changeComponentFlags(int)));

    QVBoxLayout *symPatLayout = new QVBoxLayout;
    foreach (QAbstractButton *button, m_symbolPattern->buttons())
        symPatLayout->addWidget(button);

    QGroupBox *symPatGroup = new QGroupBox(tr("Component type"));
    symPatGroup->setLayout(symPatLayout);

    m_general->addRow(symPatGroup);

    connect(m_libModel, SIGNAL(libraryAdded(QedaLibrary*)),
            m_general, SLOT(addLibrary(QedaLibrary*)));
    connect(m_libModel, SIGNAL(libraryRemoved(QedaLibrary*)),
            m_general, SLOT(removeLibrary(QedaLibrary*)));
    connect(m_general, SIGNAL(dataChanged()), this, SLOT(setModified()));

    QWidget *w = new QWidget;
    w->setLayout(m_general);

    return w;
}

QWidget *ComponentWindow::createPatternTab()
{
    QWidget *w = new QWidget;
    return w;
}

QWidget *ComponentWindow::createSymbolTab()
{
    QWidget *w = new QWidget;
    return w;
}

void ComponentWindow::restore()
{
    foreach (QAbstractButton* b, m_symbolPattern->buttons())
        if (m_symbolPattern->id(b) == m_component->flags(QedaComponent::SymbolPatternFlags))
            b->setChecked(true);
    updateTabs(m_symbolPattern->checkedId());
    m_general->dataFrom(m_component);
    setModified(false);
}

void ComponentWindow::setModified(bool modifiedFlag)
{
    m_modified = modifiedFlag;
    m_applyButton->setEnabled(m_modified);
    m_restoreButton->setEnabled(m_modified);
    emit modified(m_modified);
}

void ComponentWindow::updateTabs(int componentFlags)
{
    m_propertiesTab->clear();
    m_propertiesTab->addTab(m_tabs["General"], tr("General"));
    m_propertiesTab->addTab(m_tabs["PinManager"], tr("Pin manager"));
    if (componentFlags & QedaComponent::SymbolFlag)
        m_propertiesTab->addTab(m_tabs["Symbol"], tr("Symbol"));
    if (componentFlags & QedaComponent::PatternFlag)
        m_propertiesTab->addTab(m_tabs["Pattern"], tr("Pattern"));
}
